home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_064 / examples / old2ilbm.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  249 lines

  1. /***************************************************************************
  2. *  Old2ILBM.c v2 --  Convert old format .pic file to ILBM file
  3. *                    by Carolyn Scheppner  CBM  02/86
  4. *     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  5. *
  6. *         Note: This program does not create a .info file for
  7. *               the new IFF file. If need an icon for the file,
  8. *               (ex. for Graphicraft), copy the .info file
  9. *               for another picture and rename it to your
  10. *               IFF file name with a .info suffix.
  11. *
  12. * Alink information:
  13. * FROM     LIB:LStartup.obj,Old2ILBM.o,iffw.o,ilbmw.o,packer.o
  14. * TO       Old2ILBM
  15. * LIBRARY  LIB:lc.lib,LIB:amiga.lib
  16. *
  17. *  Note: B: assigned to libs:  and SRC: assigned to dir of .o files
  18. *
  19. ***************************************************************************/
  20.  
  21. #include <exec/types.h>
  22. #include <exec/memory.h>
  23. #include <libraries/dos.h>
  24. #include <graphics/gfxbase.h>
  25. #include <graphics/rastport.h>
  26. #include <graphics/gfx.h>
  27. #include <graphics/view.h>
  28.  
  29. #include <intuition/intuition.h>
  30. #include <intuition/intuitionbase.h>
  31.  
  32. #include <iff/ilbm.h>
  33.  
  34. #define bufSize 512
  35.  
  36. struct IntuitionBase *IntuitionBase;
  37. struct GfxBase       *GfxBase;
  38. ULONG  DosBase;
  39.  
  40. struct BitMap   oldBitMap;
  41. struct OldHeader {
  42.           WORD  filetype;      /* 0x0000 = pic */
  43.           WORD  compression;   /* 0x0000 (not implemented) */
  44.           WORD  planes;        /* 0x0000 or x0005 means 5 planes */
  45.           WORD  mini;          /* 0x0000 */
  46.           ULONG datasize;      /* 0x0000FA00 for 320x200, 5 planes */
  47.           WORD  xorig;         /* 0x0000 */
  48.           WORD  yorig;         /* 0x0000 */
  49.           WORD  height;        /* in bits */
  50.           WORD  width;         /* may in bytes ! */
  51.           } oldHeader = {0};
  52. struct OldColor {              /* Old .pic colorTable format */
  53.           BYTE  red;           /* 0x00 to 0x0f  R */
  54.           BYTE  green;         /* 0x00 to 0x0f  G */
  55.           BYTE  blue;          /* 0x00 to 0x0f  B */
  56.           BYTE  pad;           /* 0x00            */
  57.           } oldColor = {0};
  58.  
  59. WORD   colorTable[32];         /* Amiga colorTable, each WORD is 0x0RGB */
  60.  
  61. int    depth, width, height, i;
  62.  
  63. /* main() */
  64. void main(argc,argv)
  65. int  argc;
  66. char **argv;
  67.    {
  68.    LONG oldfile;
  69.    LONG newfile;
  70.    IFFP iffp = NO_FILE;
  71.    WORD colortemp;
  72.    int  planebytes;
  73.  
  74.    printf("\nOLd2ILBM v2 - Converts old format .pic to IFF\n\n");
  75.  
  76.    if (argc < 3)
  77.       printf("   Usage: 'Old2ILBM oldfilename newfilename'\n\n");
  78.    else
  79.       {
  80.       if ((IntuitionBase =
  81.        (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  82.          cleanexit(-1);
  83.  
  84.       if ((GfxBase =
  85.        (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  86.          cleanexit(-1);
  87.  
  88.       if ((DosBase = OpenLibrary("dos.library",0))==NULL)
  89.          cleanexit(-1);
  90.  
  91.       if ((oldfile = Open(argv[1], MODE_OLDFILE))==NULL)
  92.          printf("Can't open %s\n\n",argv[1]);
  93.       else
  94.          {
  95.          if ((newfile = Open(argv[2], MODE_NEWFILE))==NULL)
  96.             printf("Can't open %s\n\n",argv[2]);
  97.          else
  98.             {
  99.             printf(" Note: This program does not create a .info file\n");
  100.             printf("       for the IFF picture file.  If you need an\n");
  101.             printf("       icon, copy a picture .info file from your\n");
  102.             printf("       IFF paint package and give it the same name\n");
  103.             printf("       as your picture file adding a .info suffix.\n\n");
  104.  
  105.             Read(oldfile,(BYTE *)&oldHeader,20);
  106.  
  107.             width  = oldHeader.width;
  108.              if (width == 40)  width = 320;  /* may have been in bytes */
  109.              if (width == 80)  width = 640;
  110.             height = oldHeader.height;
  111.             depth  = oldHeader.planes;
  112.              if (depth == 0)   depth = 5;
  113.  
  114.             InitBitMap(&oldBitMap,depth,width,height);
  115.             for(i=0; i < depth; i++)
  116.                {
  117.                if((oldBitMap.Planes[i] =
  118.                 (PLANEPTR)AllocRaster(width,height)) == NULL)
  119.                   {
  120.                   printf("Can't allocate RAM for bitmap.\n");
  121.                   cleanexit(-1); 
  122.                   }
  123.                }
  124.  
  125.             printf("\nConverting '%s' to IFF '%s'...\n\n", argv[1],argv[2]);
  126.  
  127.             for (i=0; i < (1<<depth); i++)
  128.                {
  129.                 colortemp = 0;
  130.                 Read(oldfile,(BYTE *)&oldColor,4);
  131.                 colortemp = oldColor.red;
  132.                 colortemp = colortemp << 4;
  133.                 colortemp |= oldColor.green;
  134.                 colortemp = colortemp << 4;
  135.                 colortemp |= oldColor.blue;
  136.                 colorTable[i] = colortemp;
  137.                 }
  138.  
  139.             planebytes = (int)((width/8)*height);
  140.             for(i=0; i < depth; i++)
  141.                {
  142.                Read(oldfile,oldBitMap.Planes[i],planebytes);
  143.                }
  144.  
  145.             Write(newfile,"x",1);  /* so Seek to beginning works ? */
  146.  
  147.             iffp = PutPicture(newfile, &oldBitMap, &colorTable[0]);
  148.  
  149.             Close(newfile);
  150.             }
  151.          Close(oldfile);
  152.          }
  153.  
  154.       cleanup();
  155.       }
  156.    }
  157.  
  158.  
  159. cleanexit(error)
  160. int error;
  161.    {
  162.    cleanup();
  163.    exit(error);
  164.    }
  165.  
  166. cleanup()
  167.    {
  168.    for (i=0; i < depth; i++)
  169.       {
  170.       if(oldBitMap.Planes[i] != NULL)
  171.        FreeRaster(oldBitMap.Planes[i],width,height);
  172.       }
  173.  
  174.    if (DosBase) CloseLibrary(DosBase);
  175.    if (GfxBase) CloseLibrary(GfxBase);
  176.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  177.    }
  178.  
  179. /** PutPicture() ***********************************************************
  180.  *
  181.  * Put a picture into an IFF file.
  182.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  183.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  184.  * write out all the bitplanes in the BitMap.
  185.  *
  186.  ***************************************************************************/
  187. Point2D nullPoint = {0, 0};
  188.  
  189. IFFP PutPicture(file, bitmap, colorMap)
  190.       LONG file;  struct BitMap *bitmap;  WORD *colorMap;
  191.    {
  192.    BYTE buffer[bufSize];
  193.    return( PutAnILBM(file, bitmap, NULL,
  194.            colorMap, bitmap->Depth, &nullPoint,
  195.            buffer, bufSize) );
  196.    }    
  197.  
  198.    
  199. /** PutAnILBM() ************************************************************
  200.  *
  201.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  202.  * This version works for any display mode (C. Scheppner).
  203.  *
  204.  * Normal return result is IFF_OKAY.
  205.  *
  206.  * The utility program IFFCheck would print the following outline of the
  207.  * resulting file:
  208.  *
  209.  *   FORM ILBM
  210.  *     BMHD
  211.  *     CMAP
  212.  *     BODY       (compressed)
  213.  *
  214.  ***************************************************************************/
  215. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  216.  
  217. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth, xy, buffer, bufsize)
  218.       LONG file;  struct BitMap *bitmap;  BYTE *mask;  WORD *colorMap;
  219.       UBYTE depth;  Point2D *xy;
  220.       BYTE *buffer;  LONG bufsize;
  221.    {
  222.    BitMapHeader bmHdr;
  223.    GroupContext fileContext, formContext;
  224.    IFFP ifferr;
  225.    WORD pageWidth, pageHeight;
  226.  
  227.    pageWidth  = (bitmap->BytesPerRow) << 3;
  228.    pageHeight = bitmap->Rows;
  229.  
  230.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  231.                       cmpByteRun1, 0, pageWidth, pageHeight);
  232.    /* You could write an uncompressed image by passing cmpNone instead
  233.     * of cmpByteRun1 to InitBMHdr. */
  234.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  235.    if (mask != NULL) bmHdr.masking = mskHasMask;
  236.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  237.  
  238.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  239.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  240.  
  241.    CkErr( PutBMHD(&formContext, &bmHdr) );
  242.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  243.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  244.  
  245.    CkErr( EndWGroup(&formContext) );
  246.    CkErr( CloseWGroup(&fileContext) );
  247.    return( ifferr );
  248.    }    
  249.